;;;; Cross compilation   -*- mode: scheme; coding: utf-8; -*-
;;;;
;;;; 	Copyright (C) 2010-2014 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (tests cross-compilation)
  #:use-module (test-suite lib)
  #:use-module (rnrs bytevectors)
  #:use-module (system vm elf)
  #:use-module (system base compile)
  #:use-module (system base target))

(define (test-triplet cpu vendor os)
  (let ((triplet (string-append cpu "-" vendor "-" os)))
    (pass-if (format #f "triplet ~a" triplet)
      (with-target triplet
        (lambda ()
          (and (string=? (target-cpu) cpu)
               (string=? (target-vendor) vendor)
               (string=? (target-os) os)))))))

(define (native-cpu)
  (with-target %host-type target-cpu))

(define (native-os)
  (with-target %host-type target-os))

(define (native-word-size)
  ((@ (system foreign) sizeof) '*))

(define (test-target triplet endian word-size)
  (pass-if (format #f "target `~a' honored" triplet)
    (with-target triplet
      (lambda ()
        (let ((word-size
               ;; When the target is the native CPU, rather trust
               ;; the native CPU's word size.  This is because
               ;; Debian's `sparc64-linux-gnu' port, for instance,
               ;; actually has a 32-bit user-land, for instance (see
               ;; <http://www.debian.org/ports/sparc/#sparc64bit>
               ;; for details.)
               (if (and (string=? (native-cpu) (target-cpu))
                        (string=? (native-os) (target-os)))
                   (native-word-size)
                   word-size))
              (bv (compile '(hello-world) #:to 'bytecode)))
          (and=> (parse-elf bv)
                 (lambda (elf)
                   (and (equal? (elf-byte-order elf) endian)
                        (equal? (elf-word-size elf) word-size)))))))))

(with-test-prefix "cross-compilation"

  (test-triplet "i586" "pc" "gnu0.3")
  (test-triplet "x86_64" "unknown" "linux-gnu")
  (test-triplet "x86_64" "unknown" "kfreebsd-gnu")

  (test-target "i586-pc-gnu0.3" (endianness little) 4)
  (test-target "x86_64-pc-linux-gnu" (endianness little) 8)
  (test-target "powerpc-unknown-linux-gnu" (endianness big) 4)
  (test-target "sparc64-unknown-freebsd8.2" (endianness big) 8)

  (test-target "mips64el-unknown-linux-gnu"       ; n32 or o32 ABI
               (endianness little) 4)
  (test-target "mips64el-unknown-linux-gnuabi64"  ; n64 ABI (Debian tuplet)
               (endianness little) 8)
  (test-target "x86_64-unknown-linux-gnux32"      ; x32 ABI (Debian tuplet)
               (endianness little) 4)
  (test-target "arm-unknown-linux-androideabi"
               (endianness little) 4)
  (test-target "armeb-unknown-linux-gnu"
               (endianness big) 4)
  (test-target "aarch64-linux-gnu"
               (endianness little) 8)
  (test-target "aarch64_be-linux-gnu"
               (endianness big) 8)

  (pass-if-exception "unknown target" exception:miscellaneous-error
    (with-target "fcpu-unknown-gnu1.0"
      (lambda ()
        (compile '(ohai) #:to 'bytecode)))))

;; Local Variables:
;; eval: (put 'with-target 'scheme-indent-function 1)
;; End:
